/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.dorian.atomicuml.models;

import java.util.Locale;
import org.springframework.context.MessageSource;
import org.springframework.validation.Errors;
import org.springframework.validation.ValidationUtils;
import org.springframework.validation.Validator;

/**
 *
 * @author cedric.creusot
 */
public class RegisterValidator implements Validator {

    private MessageSource messageSource;
    
    public RegisterValidator(MessageSource messageSource)
    {
        super();
        this.messageSource = messageSource;
    }
    
    @Override
    public boolean supports(Class<?> type) {
        return (UserInfoData.class.isAssignableFrom(type));
    }

    @Override
    public void validate(Object o, Errors errors) {
        try
        {
            final String ERRORS1 = this.messageSource.getMessage("register.errors.empty", null, Locale.FRENCH);
            final String ERRORS2 = this.messageSource.getMessage("register.errors.password", null, Locale.FRENCH);
            final String ERRORS3 = this.messageSource.getMessage("register.errors.mail", null, Locale.FRENCH);
    

            UserInfoData uid = (UserInfoData)o;

            ValidationUtils.rejectIfEmptyOrWhitespace(errors,
                                                      "firstname", 
                                                      "errors.required",
                                                      new Object[] {"firstname"},
                                                      ERRORS1);

            ValidationUtils.rejectIfEmptyOrWhitespace(errors,
                                                      "lastname", 
                                                      "errors.required",
                                                      new Object[] {"lastname"},
                                                      ERRORS1);

            ValidationUtils.rejectIfEmptyOrWhitespace(errors,
                                                      "mail", 
                                                      "errors.required",
                                                      new Object[] {"mail"},
                                                      ERRORS1);

            ValidationUtils.rejectIfEmptyOrWhitespace(errors,
                                                      "password", 
                                                      "errors.required",
                                                      new Object[] {"password"},
                                                      ERRORS1);

            ValidationUtils.rejectIfEmptyOrWhitespace(errors,
                                                      "pwd_verif", 
                                                      "errors.required",
                                                      new Object[] {"pwd_verif"},
                                                      ERRORS1);

            if (!uid.isMailValid())
            {
                errors.rejectValue("mail",
                                   "errors.required",
                                   new Object[] {"mail"},
                                   ERRORS3);
            }

            if (!uid.isPasswordValid())
            {
                errors.rejectValue("pwd_verif",
                                   "errors.required",
                                    new Object[] {"pwd_verif"},
                                    ERRORS2);
            }
        }
        catch (Exception e)
        {
            System.out.println("Validator error :" + e);
        }
    }
    
}
